package classeMetier;
import java.sql.*;
import java.util.*;
public class MessageDuForum {
	private ArrayList messages=new ArrayList();
	public ArrayList ensembledessujets(DataManager connecteur) throws ClassNotFoundException, SQLException {
		try {
			Connection connexion=connecteur.getConnection();
			String requete=	"SELECT ID, AUTEUR, MAX(DATECREATION) AS DATECREATION,"
					+ " SUJET, TEXTE FROM MESSAGE GROUP BY SUJET"
					+ " ORDER BY DATECREATION DESC";
			PreparedStatement statement=connexion.prepareStatement(requete);
			ResultSet resultat=statement.executeQuery();
			while (resultat.next())
			{
				Message message=new Message();
				message.setId(resultat.getInt("ID"));
				message.setAuteur(resultat.getString("AUTEUR"));
				message.setDateCreation(resultat.getTimestamp("DATECREATION"));
				message.setSujet(resultat.getString("SUJET"));
				message.setTexte(resultat.getString("TEXTE"));
				this.messages.add(message);
			}
		}
		catch (SQLException e) {
			System.out.println("Could not connect to DB: " + e.getMessage());
		}
		return messages;

	}
}
